﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Navigation;
using Microsoft.SharePoint;
using System.Web;
using System.Web.UI.WebControls;
using System.Configuration;

namespace CustomNavigationDelegate
{
    class NavigationItem
    {
        public string Title { get; set; }
        public string Url { get; set; }
        public int Order { get; set; }
        public int Id { get; set; }
    }

    class ParentNavigationItem : NavigationItem
    {
        public List<NavigationItem> children;

        public ParentNavigationItem()
        {
            children = new List<NavigationItem>();
        }

        public void AddChild(NavigationItem child)
        {
            children.Add(child);
        }
    }


    class CustomNavigation : PortalSiteMapProvider
    {
        List<ParentNavigationItem> navList = null;

        private void CreateNavigationItems()
        {
            navList = new List<ParentNavigationItem>();                        
            string url = "http://" + HttpContext.Current.Server.MachineName;            

            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {                    
                    SPList list = web.Lists.TryGetList("Navigation");
                    if (list != null)
                    {
                        SPQuery query = new SPQuery();
                        query.Query = "<Query/>";

                        SPListItemCollection listItemColl = list.GetItems(query);
                        
                        //get all parent nav items
                        foreach (SPListItem item in listItemColl)
                        {
                            string parent = (string)item["Parent"];
                            if (parent == null)
                            {
                                ParentNavigationItem parentNav = new ParentNavigationItem();
                                parentNav.Title = (string)item["Title"];
                                parentNav.Url = (string)item["URL"];
                                parentNav.Id = (int)item["ID"];
                                parentNav.Order = Convert.ToInt32((double)item["Order0"]);
                                navList.Add(parentNav);
                            }
                        }

                        //get all child nav items
                        int count = navList.Count;
                        for (int i = 0; i < count; i++)
                        {                            
                            foreach (SPListItem item in listItemColl)
                            {
                                if ((string)item["Parent"] == navList[i].Id+";#"+navList[i].Title)
                                {
                                    NavigationItem navItem = new NavigationItem();
                                    navItem.Title = (string)item["Title"];
                                    navItem.Url = (string)item["URL"];
                                    navItem.Order = Convert.ToInt32((double)item["Order0"]);
                                    navList[i].AddChild(navItem);
                                }
                            }
                        }
                    }
                }
            }
        }

        public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
        {

            PortalSiteMapNode pNode = node as PortalSiteMapNode;            

            if (pNode != null)
            {
                CreateNavigationItems();
                navList.Sort((a, b) => a.Order.CompareTo(b.Order));
                foreach (ParentNavigationItem item in navList)
                {
                    item.children.Sort((a, b) => a.Order.CompareTo(b.Order));
                }
            }                        
            
            if ((pNode != null))
            {
                if (pNode.Type == NodeTypes.Area)
                {                                     
                    SiteMapNodeCollection parentNodes = new SiteMapNodeCollection();
                    
                    foreach (ParentNavigationItem item in navList)
                    {                        
                        SiteMapNode parentNode = new SiteMapNode(this, item.Url, item.Url, item.Title);                     
                        parentNodes.Add(parentNode);                      

                        if (item.children.Count > 0)
                        {
                            SiteMapNodeCollection childNodes = new SiteMapNodeCollection();                            
                            foreach (NavigationItem childItem in item.children)
                            {
                                SiteMapNode childNode = new SiteMapNode(this, childItem.Url, childItem.Url, childItem.Title);                                            
                                childNodes.Add(childNode);
                            }
                            parentNode.ChildNodes = childNodes;                            
                        }
                    }
                    return parentNodes;
            }
            else
                return base.GetChildNodes(pNode);
        }
        else
            return new SiteMapNodeCollection();
        }                 
    }        
}
